#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import rospy
from ros_demo.srv import AddTwoInts, AddTwoIntsRequest

def add_two_ints_client(x, y):
    # 初始化ROS节点
    rospy.init_node('add_two_ints_client_py')
    
    # 等待服务可用
    rospy.loginfo("等待服务可用...")
    rospy.wait_for_service('add_two_ints')
    
    try:
        # 创建服务代理
        add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
        
        # 调用服务
        rospy.loginfo(f"发送服务请求: {x} + {y}")
        resp = add_two_ints(x, y)
        rospy.loginfo(f"服务调用成功，结果: {resp.sum}")
        
    except rospy.ServiceException as e:
        rospy.logerr(f"服务调用失败: {e}")

if __name__ == "__main__":
    if len(sys.argv) == 3:
        x = int(sys.argv[1])
        y = int(sys.argv[2])
    else:
        print("用法: add_two_ints_client X Y")
        sys.exit(1)
    
    add_two_ints_client(x, y)
